class WrongNumberOfPlayersError < StandardError; end
class NoSuchStrategyError < StandardError; end

def rps_game_winner(game)
	raise WrongNumberOfPlayersError unless game.length == 2
	
	strategy = [game[0][1].downcase, game[1][1].downcase];
	raise NoSuchStrategyError unless strategy[0] =~ /\A[rps]\z/ 
	raise NoSuchStrategyError unless strategy[1] =~ /\A[rps]\z/ 

	outcome = ["pr", "sp", "rs", "pp", "ss", "rr", "rp", "ps", "sr"]
		
	if (outcome.index(strategy.join) <= 5)
		#player one wins
		return game[0]
	else
		#player two wins
		return game[1]
	end
	
end

def rps_tournament_winner(input)

	if input.flatten.length < 4 
		#winner is found - only one player
		return input 
	end	
	#check the winner of the first sub_table
	winner1 = rps_tournament_winner(input[0])
	winner2 = rps_tournament_winner(input[1])
	return rps_game_winner([winner1, winner2]);

end	



	input = [ [ "Armando", "P" ], [ "Dave", "S" ] ]
	input1 = 
[
[
[ ["Armando", "P"], ["Dave", "S"] ],
[ ["Richard", "R"], ["Michael", "S"] ],
],
[
[ ["Allen", "S"], ["Omer", "P"] ],
[ ["David E.", "R"], ["Richard X.", "P"] ]
]
]
		
	puts("tournament won by " + rps_tournament_winner(input1)[0])
	
ARGV.each do |a|
	#puts(palindrome?(a))
	puts(rps_game_winner(a))
end


